/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  表单-select基础类
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-07-05
 * @param
 * opt{
 * 	'el': '',       // select标签
 * 	'defaultNum':'',// 缺省项
 * 	'size':'',		// 缺省可见项数
 * 	'multiple':'',	// 缺省是否多选
 * 	'isMust': '',   // 是否必填
 * 	'isChange': '' // 是否实时检测
 *  'defaultValue'  默认选中项
 * }
 * @example
 * var a = new STK.module.form.formSelect(opt);
 */
$require('mod.getStrLength')
LJS.define('form.formSelect',function(ljs){
    var getStrLength = ljs.mod.getStrLength;
     var formSelect = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // select标签
        that.el = opt.el;
        // 缺省选项,从0开始,默认是0
        that.defaultNum = opt.defaultNum || null;
        // 可见项数,默认是1
        that.size = parseInt(opt.size) || 1;
        // 是否可以选择多项
        that.multiple = opt.multiple || null;
        // 是否是必选项,默认不是必选(供外部重写)
        that.isMust = opt.isMust || 0;
        // 是否实时判断合法性,默认不开启(供外部重写)
        that.isChange = opt.isChange || 0;
        that.defaultValue = opt.defaultValue || null;
    };
    formSelect.prototype = {
        init: function(){
            var that = this;
            // 如果有默认选项,则将默认选项选中
            // 通过值设置选 中
            that.defaultValue?that.setSelectedByValue(that.defaultValue):that.setSelected(that.defaultNum);
            // 设置可见项数
            that.setSize(that.size);
            // 如果是多选,将多选功能开启或关闭
            that.multiple !== null ? that.setMultiple(that.multiple) : '';
        },
        /**
         * 设置option内容(暂时只支持单项)
         * @param {Object} opt
         * opt{
         * 	selectIndex:'', // 设置当前选中项内容(可选)
         * 	optionIndex:'', // 设置某项内容(可选)
         *	text:'',		// 要设置的内容(必选)
         *	value:''		// 要设置的value(必选)
         * }
         */
        setValue: function(opt){
            var that = this;
            if (!opt || typeof opt.text === 'undefined' || typeof opt.value === 'undefined') 
                return false;
            if (opt.selectIndex) {
                that.replace(that.el.selectedIndex, opt.text, opt.value);
            } else if (opt.optionIndex || opt.optionIndex === 0) {
                that.replace(opt.optionIndex, opt.text, opt.value);
            } else {
                for (var i = 0, len = that.getLength(); i < len; i++) {
                    that.el.options[i].value === opt.value ? that.el.options[i].text = opt.text : '';
                }
            }
        },
        /**
         * 添加一项
         * @param {Number} optionIndex 要添加的位置(必选)
         * @param {String || Number} text 显示文字(必选)
         * @param {String || Number} value 值(必选)
         * @param {Boolean} defaultSelected 是否设置为默认选项(可选)
         * @param {Boolean} selected 添加后是否选择(可选)
         */
        add: function(optionIndex, text, value, defaultSelected, selected){
            var that = this;
            if (optionIndex < 0 || optionIndex > that.getLength()) 
                return false;
            var _addOption = new Option(text, value, defaultSelected, selected);
            // IE需先加入到select中
            that.el.options[that.getLength()] = _addOption;
            that.el.insertBefore(_addOption, that.el.options[optionIndex]);
        },
        /**
         * 替换一项
         * @param {Object} optionIndex 要替换的位置(必选)
         * @param {Object} text 显示文字(必选)
         * @param {Object} value 值(必选)
         * @param {Object} defaultSelected 是否设置为默认选项(可选)
         * @param {Object} selected 添加后是否选择(可选)
         */
        replace: function(optionIndex, text, value, defaultSelected, selected){
            var that = this;
            if (optionIndex < 0 || optionIndex > that.getLength()) 
                return false;
            that.el.options[optionIndex] = new Option(text, value, defaultSelected, selected);
        },
        /**
         * 删除一项
         * @param {Object} optionIndex 要删除的位置(必选)
         */
        del: function(optionIndex){
            var that = this;
            if (optionIndex < 0 || optionIndex > that.getLength()) 
                return false;
            that.el.options[optionIndex] = null;
        },
        /**
         * 自身的change事件扩展
         * @param {Object} opt
         * opt{
         * 	callback:function(obj){
         * 		//单选 obj = option;
         * 		//多选 obj = options;
         * 	}
         * }
         */
        change: function(opt){
            var that = this;
            $.addEvent(that.el, 'change', function(){
                opt && opt.callback ? opt.callback(that.getSelectOption()) : '';
            });
        },
        /**
         * 设置选择项
         * @param {Object} index
         */
        setSelected: function(index){
            var that = this;
            if ((index || index === 0) && that.el.options[index]) {
                that.el.options[index].selected = 'selected';
            }
        },
        /**
         * 通过值设置选择项
         * @param String val 
         */
        setSelectedByValue: function(val){
            var that = this;
            //console.log($(that.el).find('option[value="客厅"]'))
            $(that.el).find('option[value="'+val+'"]').attr('selected', 'selected');
        },
        /**
         * 获得当前选中的option
         * @return obj 单个option或者options数组
         */
        getSelectOption: function(){
            var that = this;
            if (that.getType() == 'select-one') {
                // 单选,返回单选option对象
                var _choice = that.el.selectedIndex;
                return that.el.options[_choice];
            } else {
                // 多选,返回多选options数组
                var _choiceArr = [];
                for (var i = 1, len = that.getLength(); i < len; i++) {
                    var _option = that.el.options[i];
                    if (_option.selected) 
                        _choiceArr.push(_option);
                }
                return _choiceArr;
            }
        },
        /**
         * 读取select有多少项
         * @return Number 项数
         */
        getLength: function(){
            var that = this;
            return that.el.options.length;
        },
		
        /**
         * 设置list可见项数
         * @param {Number} size 可见项数(必选)
         * 由于web-kit除单项外,至少显示4项的高度,
         * 为保持一致,其它浏览器向它兼容
         */
        setSize: function(size){
            var that = this;
            if (that.multiple) {
                // 如果是多选,可见项不能小于4
                that.el.size = size < 4 ? 4 : size;
            } else {
                // 如果不是多选,可见项是1或者大于等于4的数
                that.el.size = size > 1 && size < 4 ? 4 : size;
            }
        },
        /**
         * 设置是否为多选
         * @param {Boolean} bool true 多选 || false 单选
         * 由于safira在多选状态打开时,options全显示,
         * 而IE、FF、chrome默认显示4项,
         * 为保持一致,将safira也设置为默认显示4项
         */
        setMultiple: function(bool){
            var that = this;
            that.el.multiple = bool;
            // 如果是多选,可见项不能小于4
            bool && that.size < 4 ? that.setSize(4) : '';
        },
        /**
         * 获得select是否是多选
         * @return String select-multiple 多选 || select-one 单选
         */
        getType: function(){
            var that = this;
            return that.el.type;
        },
        /**
         * 置为禁用
         * @param {Boolean} bool true || false
         */
        setDisabled: function(bool){
            var that = this;
            bool ? that.el.disabled = 'disabled' : that.el.disabled = '';
        },
        /**
         * 读取input是否可读
         * @return Boolean true || false
         */
        getReadOnly: function(){
            var that = this;
            return that.el.readOnly;
        },
        /**
         * 设置input是否可读
         * @param {Boolean} bool true || false
         */
        setReadOnly: function(bool){
            var that = this;
            that.el.readOnly = bool;
        },
        /**
         * 以下都为空方法,供外部重写
         */
        check: function(){
        
        },
        showTip: function(){
        
        },
        showError: function(){
        
        },
        hide: function(){
        
        }
    };
    return formSelect;
});
